package com.zdzh.socketio.socket.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zdzh.socketio.socket.session.ClientSessionHolder;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class ClientListener {

    @OnConnect
    public void onConnect(SocketIOClient client) {
        log.info("连接socketClient成功{}", JSON.toJSONString(client.getHandshakeData().getUrlParams()));
        ClientSessionHolder.insert(client);
    }

    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        log.info("socketClient断开：{}",  JSON.toJSONString(client.getHandshakeData().getUrlParams()));
        ClientSessionHolder.delete(client);
    }

    @OnEvent("confirmConnection")
    public void confirmConnection(SocketIOClient client, AckRequest request, JSONObject json) {
        log.info("confirmConnection事件参数：{}", json);
        request.sendAckData("我收到你连接成功的消息了!");
    }


    @OnEvent("sendMsg")
    public void sendMsg(SocketIOClient client, AckRequest request, JSONObject json) {
        log.info("sendMsg事件参数：{}", json);
    }
}
